package com.xyzwps.website.modules.user.service;

import com.xyzwps.website.modules.user.dao.UserDao;
import com.xyzwps.website.modules.user.entity.User;
import com.xyzwps.website.modules.user.entity.UserStatus;
import jakarta.inject.Singleton;
import org.slf4j.Logger;

import java.time.Instant;
import java.util.Optional;

@Singleton
public class UserService {

    private static final Logger log = org.slf4j.LoggerFactory.getLogger(UserService.class);

    private final UserDao userDao;

    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public void createUserByPhone(String phone) {
        var newUser = new User()
                .setPhone(phone)
                .setDisplayName("xx")
                .setAvatar("")
                .setPassword(null)
                .setEmail(null)
                .setStatus(UserStatus.ACTIVE)
                .setCreateTime(Instant.now())
                .setUpdateTime(Instant.now());
        var id = userDao.insert(newUser);
        log.info("create user: {}", id);
    }

    public Optional<User> getUserByPhone(String phone) {
        return userDao.findByPhone(phone);
    }
}
